package com.neuedu.his.controller;

import com.neuedu.his.po.*;
import com.neuedu.his.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/register")
public class RegisterController {



    @Autowired
    private RegisterService registerService;

    // 保存挂号信息
    @PostMapping("/save")
    public Map<String, Object> saveRegistration(@RequestBody Register register) {
        return registerService.calculateAndSaveRegistration(register);
    }

    // 获取科室列表
    @GetMapping("/departments")
    public Map<String, Object> getDepartments() {
        List<Department> departments = registerService.getDepartments();

        // 构建 id->name 的映射
        Map<Integer, String> deptMap = departments.stream()
                .collect(Collectors.toMap(Department::getId, Department::getDeptname));

        Map<String, Object> result = new HashMap<>();
        result.put("departments", departments);
        result.put("deptMap", deptMap);
        return result;
    }

    // 获取科室下的医生
    @GetMapping("/doctors/{deptId}")
    public Map<String, Object> getDoctorsByDeptId(@PathVariable Integer deptId) {
        List<User> doctors = registerService.getDoctorsByDeptId(deptId);
        Map<String, Object> result = new HashMap<>();

        result.put("doctors", doctors.stream().map(doctor -> {
            Map<String, Object> doc = new HashMap<>();
            doc.put("id", doctor.getId());
            doc.put("realname", doctor.getRealName());
            doc.put("deptid", doctor.getDeptID());
            return doc;
        }).collect(Collectors.toList()));

        if (doctors.isEmpty()) {
            result.put("message", "该科室暂无可用医生");
        }
        return result;
    }

    // 获取挂号级别列表
    @GetMapping("/registLevels")
    public List<Registlevel> getRegistLevels() {
        return registerService.getRegistLevels();
    }

    // 获取性别选项列表
    @GetMapping("/genders")
    public List<Constantitem> getGenders() {
        return registerService.getGenders();
    }

    // 计算总费用
    @GetMapping("/calculateFee")
    public Map<String, Object> calculateTotalFee(@RequestParam Integer registLevelId,
                                                 @RequestParam boolean isBook) {
        BigDecimal fee = registerService.calculateTotalFee(registLevelId, isBook);
        Map<String, Object> result = new HashMap<>();
        result.put("totalFee", fee);
        return result;
    }
}